package cn.sfa.common.quartzJob;

import java.net.URI;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import microsoft.exchange.webservices.data.Appointment;
import microsoft.exchange.webservices.data.AppointmentType;
import microsoft.exchange.webservices.data.BodyType;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.SendInvitationsMode;
import microsoft.exchange.webservices.data.StringList;
import microsoft.exchange.webservices.data.TimeZoneDefinition;
import microsoft.exchange.webservices.data.WebCredentials;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.hsqldb.Types;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.sfa.common.quartzJob.AccountJob.AccountEntity;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.ConfigUtil;
import cn.sfa.entity.log.SaleJobLogs;
import cn.sfa.entity.quartz.SaleQuartzMessage;
import cn.sfa.entity.routine.SaleMessagestake;
import cn.sfa.entity.smsmessage.SaleSmsMessage;
import cn.sfa.service.log.LogService;
import cn.sfa.service.routine.CalendarService;
import cn.sfa.service.routine.ExChangeCalendarService;

/**
 * 
 * 类名称：QuartzJob 类描述：quartz定时任务 创建人：lenovo 创建时间：2012-2-14 下午9:40:42 修改备注：
 * 
 * @version v1.0
 */
public class SMSQuartzJob {

	Logger log = LoggerFactory.getLogger(SMSQuartzJob.class);
	private LogService logService;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	private QuartzMessageService smsService;

	public void setSmsService(QuartzMessageService smsService) {
		this.smsService = smsService;
	}

	public QuartzMessageService getSmsService() {
		return smsService;
	}
	 
	public void sendSMS() {
		 
		List<SMSmessageEntity> result = smsService.getSmsMessage();
		 
		for (SMSmessageEntity message : result) {
			smsService.setSMSSend(message.getRecvmobileno(),message.getSmscontext());
	    	SaleJobLogs jobLogs = new SaleJobLogs();
			jobLogs.setCreateUid("系统");
			jobLogs.setCreateDate(new Date());
			jobLogs.setStartTime(new Date());
			jobLogs.setJobName("系统");
			try{
			String userid ="82052";
			String password = "HkFs12343212";
			org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
 
			String   qurl = "http://211.147.244.114:9801/CASServer/SmsAPI/SendMessage.jsp?userid="+ userid 
			+ "&password=" + java.net.URLEncoder.encode(password,"UTF-8")
			+ "&destnumbers=" + message.getRecvmobileno()
			+ "&msg=" + java.net.URLEncoder.encode(message.getSmscontext(),"UTF-8"); 
		    org.dom4j.Document doc = reader.read(new java.net.URL(qurl));
		    String xmlstring = new String(doc.asXML().getBytes(),"GBK");
		    System.out.println(xmlstring);
		    if ("0".endsWith(doc.valueOf("/root/@return"))) {
		    
				jobLogs.setDescription("发送短信成功,手机号码为【"+message.getRecvmobileno()+"】");
				jobLogs.setVcol1("1");//1  代表成功 
				jobLogs.setStartTime(new Date());
				logService.InsertToJobLog(jobLogs);
		      }else{  
		     
				jobLogs.setDescription("发送短信失败,手机号码为【"+message.getRecvmobileno()+"】");
				jobLogs.setVcol1("0");//0  代表失败 
				jobLogs.setStartTime(new Date());
				logService.InsertToJobLog(jobLogs);
		      }

			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}


}
